<?php

namespace app\admin\controller;

use think\Db;
use think\Validate;
class Anli extends Base{
	public function index(){
		return $this->fetch();
	}
	public function index_data(){
		$input = input();
		$where=[];
		if (isset($input['keywords']) and !empty($input['keywords'])) {
			$where[] = ['name','like', '%' . $input['keywords'] . '%'];
		}
		
		if (isset($input['seach_time']) and !empty($input['seach_time'])) {
			$str = explode('~',$input['seach_time']);
		    $star_time=$str[0]? $str[0] : '';
		    $end_time=$str[1] ? $str[1] : '';
			$where[]=['add_time','between',[strtotime($star_time),strtotime($end_time)+86400]];
		}
		$page       = ($input['page']-1)*$input['limit'];
		$limit      = $input['limit'];
		
		$data = Db::name('case')
		->where($where)
		->order('add_time desc')
		->limit($page,$limit)
		->select();
		// var_dump($where);die;
		$count = Db::name('case')
		->where($where)
		->count();
		
		return hSuccess('OK', $data,$count,ceil($count/$limit));
	}
	
	public function add(){
		if($this->request->isPost()) {
			//是提交操作
			$post = $this->request->post();
			$rule = [
			    'd_title'   => 'require',
			    'x_title' => 'require',
			];
			$msg = [
			    'd_title.require'  => '大标题不能为空',
			    'x_title.require'  	=> '标题不能为空',
			];
			
			$data = [
				'img'		=>	$post['photo'],
				'd_title'	=>	$post['d_title'],
				'x_title'	=>	$post['x_title'],
				'status'	=>	isset($post['status'])?$post['status']:0,
				// 'huxiang'	=>	$post['huxiang'],
				// 'fengge'	=>	$post['fengge'],
				// 'mianji'	=>	$post['mianji'],
				// 'jiage'		=>	$post['jiage'],
				'imgs'		=>	$post['content'],
				'add_time'	=>	time(),
			];
			// var_dump($data);die;
			$validate   = Validate::make($rule,$msg);
		    //验证部分数据合法性
		    if (!$validate->check($post)) {
		        $this->error('提交失败：' . $validate->getError());
		    }
			
			$res = Db::name('case')->insert($data);
		    if($res) {
		    	return hSuccess('操作成功');
		    } else {
				return hError(302,'操作失败');
		    }
		}
		
		return $this->fetch();
	}
	public function edit(){
		// 获取参数
		$param = $this->request->param();
		// 提交操作
		if($this->request->isAjax()){
			$data = [
				'img'		=>	$param['photo'],
				'd_title'	=>	$param['d_title'],
				'x_title'	=>	$param['x_title'],
				'status'	=>	isset($param['status'])?$param['status']:0,
				// 'huxiang'	=>	$param['huxiang'],
				// 'fengge'	=>	$param['fengge'],
				// 'mianji'	=>	$param['mianji'],
				// 'jiage'		=>	$param['jiage'],
				'imgs'		=>	$param['content'],
				'update_time'	=>	time(),
			];
			
			
			$res = Db::name('case')->where('id',$param['id'])->update($data);
	        if($res){
	            return hSuccess('操作成功');
	        }else{
	            return hError('-1','操作失败');
	        }
		}
		// 展示界面
		$info = Db::name('case')->find($param['id']);
		$this->assign('info',$info);
	    return $this->fetch();
	}
	public function delete(){
		$input = $this->request->param();
		if($this->request->isAjax()) {
			$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
			$res = Db::name('case')->where('id',$id)->delete();
			if($res) {
				return hSuccess('删除成功');
			} else {
				return hError(410,'删除失败');
			}
		}
	}
	
}
?>